package com.alili_tv.system.controller;

import com.alili_tv.system.common.BaseContext;
import com.alili_tv.system.common.LoginRequired;
import com.alili_tv.system.common.R;
import com.alili_tv.system.common.RedisRank;
import com.alili_tv.system.entity.Comment;
import com.alili_tv.system.entity.Likes;
import com.alili_tv.system.exception.SystemException;
import com.alili_tv.system.service.CommentService;
import com.alili_tv.system.service.LikesService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 点赞表 前端控制器
 * </p>
 *
 * @author baomidou
 * @since 2023-03-17
 */
@RestController
@RequestMapping(value = "/likes", produces = "application/json")
public class LikesController {

    @Resource
    private LikesService likesService;

    @Resource
    private CommentService commentService;

    @Resource
    private RedisRank redisRank;

    /**
     * 点赞评论
     * @param commentId
     * @param isCancel true表示取消点赞, false表示点赞
     * @return
     */
    @LoginRequired
    @Transactional
    @PostMapping("/comment/{commentId}/{isCancel}")
    public R<String> likeComment(@PathVariable Long commentId, @PathVariable Boolean isCancel) {
        if (isCancel == null) {
            throw new SystemException("别搞...");
        }

        LambdaUpdateWrapper<Comment> updateWrapper = new LambdaUpdateWrapper<>();
        if (isCancel) {
            // 取消点赞
            LambdaQueryWrapper<Likes> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper
                    .eq(Likes::getType, 0)
                    .eq(Likes::getCvId, commentId)
                    .eq(Likes::getCreateUser, BaseContext.getCurrentId());
            likesService.remove(queryWrapper);

            // 减少点赞数
            updateWrapper.setSql("like_count = like_count - 1");
        } else {
            // 点赞
            Likes likes = new Likes();
            likes.setCvId(commentId);
            likes.setType(0);
            likesService.save(likes);

            // 添加点赞数
            updateWrapper.setSql("like_count = like_count + 1");
        }

        updateWrapper.eq(Comment::getId, commentId);
        commentService.update(updateWrapper);

        return R.success("success");
    }

    /**
     * 点赞视频
     *
     * @param videoId
     * @param isCancel true表示取消点赞, false表示点赞
     * @return
     */
    @LoginRequired
    @PostMapping("/video/{videoId}/{isCancel}")
    public R<String> likeVideo(@PathVariable Long videoId, @PathVariable Boolean isCancel) {
        if (isCancel == null) {
            throw new SystemException("别搞...");
        }
        if (isCancel) {
            // 取消点赞
            LambdaQueryWrapper<Likes> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper
                    .eq(Likes::getType, 1)
                    .eq(Likes::getCvId, videoId)
                    .eq(Likes::getCreateUser, BaseContext.getCurrentId());
            likesService.remove(queryWrapper);

            // 取消热度增加
            redisRank.incrScore(videoId, -10);
        } else {
            // 点赞
            Likes likes = new Likes();
            likes.setCvId(videoId);
            likes.setType(1);
            likesService.save(likes);

            // 增加热度
            redisRank.incrScore(videoId, 10);
        }
        return R.success("success");
    }
}
